package com.huiqiao.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huiqiao.sys.domain.LogInfo;
import com.huiqiao.sys.domain.LogInfoVo;
import com.huiqiao.sys.mapper.LogInfoMapper;
import com.huiqiao.sys.service.LogInfoService;
import com.huiqiao.sys.utils.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-23  16:26
 */

@Service
public class LogInfoServiceImpl implements LogInfoService {

    @Autowired
    private LogInfoMapper logInfoMapper;


    @Override
    public DataGridView queryAllLogInfo(LogInfoVo logInfoVo) {
        Page<Object> page = PageHelper.startPage(logInfoVo.getPage(), logInfoVo.getLimit());
        Weekend<LogInfo> weekend = Weekend.of(LogInfo.class);
        WeekendCriteria<LogInfo, Object> logInfoObjectWeekendCriteria = weekend.weekendCriteria();
        //请输入登陆名称
        if (StrUtil.isNotBlank(logInfoVo.getLoginname())){
            logInfoObjectWeekendCriteria.andLike(LogInfo::getLoginname, "%" + logInfoVo.getLoginname() + "%");
        }
        //请输入登陆IP
        if (StrUtil.isNotBlank(logInfoVo.getLoginip())){
            logInfoObjectWeekendCriteria.andLike(LogInfo::getLoginip, "%" + logInfoVo.getLoginip() + "%");
        }
        //根据开始时间和结束时间进行查询
        if (Objects.nonNull(logInfoVo.getStartTime()) && Objects.nonNull(logInfoVo.getEndTime())){
            logInfoObjectWeekendCriteria.andBetween(LogInfo::getLogintime, logInfoVo.getStartTime(), logInfoVo.getEndTime());
        }

        List<LogInfo> data = logInfoMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), data);
    }

    //删除
    @Override
    public void deleteLogInfo(LogInfoVo logInfoVo) {
        logInfoMapper.deleteByPrimaryKey(logInfoVo.getId());
    }

    //批量删除
    @Override
    public void deleteBatchLogInfo(Integer[] ids) {
        Arrays.asList(ids).stream().forEach(id -> logInfoMapper.deleteByPrimaryKey(id));
    }
}
